package com.offics.loan.interfaces.controller;

import com.offics.loan.application.dto.user.*;
import com.offics.loan.application.service.UserApplicationService;
import com.offics.loan.infrastructure.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 认证控制器
 */
@Tag(name = "认证", description = "认证相关接口")
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {

    private final UserApplicationService userApplicationService;

    /**
     * 注册
     *
     * @param registerRequest 注册请求
     * @return 注册响应
     */
    @Operation(summary = "注册", description = "用户注册")
    @PostMapping("/register")
    public Result<RegisterResponse> register(@Valid @RequestBody RegisterRequest registerRequest) {
        RegisterResponse response = userApplicationService.register(registerRequest);
        return Result.success(response);
    }

    /**
     * 登录
     *
     * @param loginRequest 登录请求
     * @param request HTTP请求
     * @return 登录响应
     */
    @Operation(summary = "登录", description = "用户登录")
    @PostMapping("/login")
    public Result<LoginResponse> login(@Valid @RequestBody LoginRequest loginRequest, HttpServletRequest request) {
        // 设置IP地址
        loginRequest.setIp(getIpAddress(request));
        LoginResponse response = userApplicationService.login(loginRequest);
        return Result.success(response);
    }

    /**
     * 发送短信验证码
     *
     * @param request 发送短信验证码请求
     * @return 发送短信验证码响应
     */
    @Operation(summary = "发送短信验证码", description = "发送短信验证码")
    @PostMapping("/sms/code")
    public Result<SendSmsCodeResponse> sendSmsCode(@Valid @RequestBody SendSmsCodeRequest request) {
        SendSmsCodeResponse response = userApplicationService.sendSmsCode(request);
        return Result.success(response);
    }

    /**
     * 通过手机验证码重置密码
     *
     * @param request 通过手机验证码重置密码请求
     * @return 是否成功
     */
    @Operation(summary = "通过手机验证码重置密码", description = "通过手机验证码重置密码")
    @PostMapping("/reset/password")
    public Result<Boolean> resetPasswordByPhone(@Valid @RequestBody ResetPasswordByPhoneRequest request) {
        boolean result = userApplicationService.resetPasswordByPhone(request);
        return Result.success(result);
    }

    /**
     * 获取IP地址
     *
     * @param request HTTP请求
     * @return IP地址
     */
    private String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
